package com.gm.commonlib.base.baseui.baseactivity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gm.commonlib.R;
import com.gm.commonlib.base.baseBean.BaseFragmentInfo;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public abstract class BaseVerticalTabLayoutFragment extends BaseFragment {
    public VerticalTabLayout mVerticalTabLayout;

   public ViewPager mViewPager;
    public View mMainView;

    public abstract List<BaseFragmentInfo> getFragments();
    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        mMainView= LayoutInflater.from(getActivity()).inflate(R.layout.fragment_base_tablayout, parent);
        initView();
        MyPagerAdapter  adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        adapter.setFragment(getFragments());
        adapter.notifyDataSetChanged();
        mViewPager.setAdapter(adapter);
        mVerticalTabLayout.setupWithViewPager(mViewPager);
        mVerticalTabLayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                for (int i = 0; i < mVerticalTabLayout.getTabCount(); i++) {
                    TabView tabView = mVerticalTabLayout.getTabAt(i);
                    TextView textView = mVerticalTabLayout.getTabAt(i).getTitleView();
                    if (tabView == tab) {
                        textView.setTextColor(getResources().getColor(R.color.color_black));
                        tab.getTabView().setBackgroundColor(getItemSelectBgColor());
                    } else {
                        textView.setTextColor(getResources().getColor(R.color.color_black));
                        tabView.getTabView().setBackgroundColor(getResources().getColor(R.color.white));
                    }

                }
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
    }
    private void initView(){
        mVerticalTabLayout=mMainView.findViewById(R.id.verticalTabLayout);
        mViewPager=mMainView.findViewById(R.id.viewPager);
    }


    public class MyPagerAdapter extends FragmentStatePagerAdapter implements TabAdapter {
        private List<BaseFragmentInfo> fragmentsList;
        public void setFragment(List<BaseFragmentInfo> fragmentsList){
            this.fragmentsList=fragmentsList;
        }
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentsList.get(position).getFragment();
        }

        @Override
        public int getCount() {
            return fragmentsList.size();
        }

        @Override
        public ITabView.TabBadge getBadge(int position) {
            return null;
        }

        @Override
        public ITabView.TabIcon getIcon(int position) {
            return null;
        }

        @Override
        public ITabView.TabTitle getTitle(int position) {

            return new QTabView.TabTitle.Builder()
                    .setTextColor(getResources().getColor(R.color.color_black),getResources().getColor(R.color.color_black))
                    .setTextSize(22)
                    .setContent(fragmentsList.get(position).getTitle())
                    .build();
        }

        @Override
        public int getBackground(int position) {
            if (position==0){
                return getItemSelectBgColor();
            }
            return 0;

        }


        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentsList.get(position).getTitle();
        }
    }

    public int getItemSelectBgColor(){
        return getResources().getColor(R.color.color_80000000);
    }

}
